package com.bop.dehong.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import cn.jpush.android.api.JPushInterface;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.LogUtils;
import com.base.dehong.util.NetWorkUtils;
import com.base.dehong.util.PreferencesUtils;
import com.base.dehong.util.RegexChk;
import com.base.dehong.util.StringUtils;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.http.model.LoginModel;
import com.bop.dehong.info.LoginInfo;
import com.bop.dehong.lock.GesturePwdActivity;
import com.bop.dehong.main.MainActivity;



public class LoginActivity extends BaseActivity {
	private EditText et_username;
	private EditText et_password;
	private String username = "";
	private String password = "";
	private final static int REQUEST_CODE = 1;
	private final static int REQUEST_FORGET = 2;
	private boolean goBack = false, go2Main = false, showForgetPwd = true;
	private LoginModel mLoginModel;
	private Long local;
	
	@Override
	protected void initUI() {
		// TODO Auto-generated method stub
		et_username = getView(R.id.et_username);
		et_password = getView(R.id.et_password);
		registerOnClickListener(getView(R.id.tv_forget_pwd),
		getView(R.id.btn_login));
	}



	
	private void loginService(String username, String password){
		showProgressDialog();
		if (mLoginModel == null) {
			mLoginModel = new LoginModel(this, loginCallBack);
		}
		String device_code = "";
//		DeviceUuidFactory factory = new DeviceUuidFactory(this);
//		device_code = factory.getDeviceUuid() + "";
		device_code = JPushInterface.getRegistrationID(this);//传RegistrationID方便后台根据指定RegistrationID推送消息
		LogUtils.e(TAG + ",device_code", "registrationId=" + device_code);
		local = System.currentTimeMillis() / 1000;
		mLoginModel.doNet(username, password, device_code);
	}
	
	
	RequestCallBack<LoginInfo> loginCallBack = new RequestCallBack<LoginInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(LoginInfo info) {
			// TODO Auto-generated method stub
			super.onSuccess(info);
			Long net = info.stamp;
			Long modify = net - local;
			if (Math.abs(modify) >= 300) {
				PreferencesUtils.putLong(LoginActivity.this, Config.TAG, modify);	
			}
			DeHongApp.mLoginEntity = info.user;
			PreferencesUtils.putString(LoginActivity.this, Config.USERNAME,
					username);
			PreferencesUtils.putString(LoginActivity.this, Config.PASSWORD,
					password);
			LogUtils.e("login-entity", "login-entity=" + DeHongApp.mLoginEntity.toString());
			String gPwd = PreferencesUtils.getString(LoginActivity.this, Config.GESTURE_PWD+username);
			if (TextUtils.isEmpty(gPwd)) {
				 Intent intent = new Intent(LoginActivity.this,GesturePwdActivity.class);
				 intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//关键的一步，设置启动模式
				 intent.putExtra(Config.GESTURE_ACTION, 1);
				 intent.putExtra("isshow", showForgetPwd);
				 startActivityForResult(intent, 100);
			}
			if (goBack) {
				setResult(RESULT_OK);
			}
			if (go2Main) {
				openActivity(MainActivity.class);
			}
			finishCurrentActivity();
		}
		
	};
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK) {
			return;
		}
		if (requestCode == 100) {
			finishCurrentActivity();
		}else{
			Bundle bundle = data.getExtras();
			username = bundle.getString("username");
			et_username.setText(username);
			et_password.setText("");
			showForgetPwd = false;
//			password = bundle.getString("password");
//			et_password.setText(password);
//			loginService(username, password);
		}
	}

	@Override
	protected void initTop() {
		View mView = getView(R.id.login_top);
		TopManager manager = new TopManager(mView);
		manager.setTitle(R.string.login);
		goBack = getIntent().getBooleanExtra(Config.GO_BACK, false);
		go2Main = getIntent().getBooleanExtra("go2main", false);
		if (!goBack) {
			manager.setLeftImageVisibility(View.GONE);
		}else {
			manager.setLeftImageVisibility(View.VISIBLE);	
		}
		manager.setRightTextVisibility(View.VISIBLE);
		manager.setRightText(R.string.register);
		manager.setRightTextOnClick(this);
	}
	
	@Override
	protected void initData() {
		username = PreferencesUtils.getString(this, Config.USERNAME);
		password = PreferencesUtils.getString(this, Config.PASSWORD);
		if (!TextUtils.isEmpty(username)) {
			et_username.setText(username);
		}
		if (!TextUtils.isEmpty(password)) {
			et_password.setText(password);
		}
	}
	
	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.activity_login;
	}

	@Override
	public void onClick(View v)
	{
		switch(v.getId()){
		case R.id.top_bar_right_txt:
			Intent intent = new Intent(this, RegisterActivity.class);
			startActivityForResult(intent, REQUEST_CODE);
			break;
		case R.id.btn_login:
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // 强制隐藏键盘
			username = getStr4TextView(et_username);
			password = getStr4TextView(et_password);
			// 检查网络
		  if (NetWorkUtils.isConnect(this)) {// 有网络
			  if (StringUtils.isEmpty(username)) {
					ToastUtils.show(this, "请输入手机号码");
					return;
				}
				if (!RegexChk.checkCellPhone(username)) {
					ToastUtils.show(this, "手机号码格式不对");
					return;
				}
				if (StringUtils.isEmpty(password)) {
					ToastUtils.show(this, "请输入登录密码");
					return;
				}
				loginService(username, password);
		  }else {
			  ToastUtils.show(this, "网络未打开");
			  finishCurrentActivity();
		}
			
			break;
		case R.id.tv_forget_pwd:
			   Intent intent1 = new Intent(this, ForgotPasswordActivity.class);
			   startActivityForResult(intent1, REQUEST_FORGET);//忘记密码回到登录页自动填入手机号
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}
}
